﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using PROJECT_BOMBER.BLL.States;



namespace PROJECT_BOMBER.BLL
{
    public class PlayState : GameState
    {
        bool pushPause;

        List<Piece> pieces = new List<Piece>();
        Win winner;

        Ghost ghost;

        SpriteFont spritetext;

        String text = "";
        String text2 = "";
        String text3 = "angle";

        Texture2D background;
        Texture2D test;

        Character character1;

        Character character2;

        Level leveltest;

        Camera2d cam;

        Song musique;

        SoundEffect soundPiece;

        float AspectRatio;
        Microsoft.Xna.Framework.Point OldWindowSize;

        Microsoft.Xna.Framework.Rectangle screenRectangle;


        public override void Initialize()
        {


            Globals.Chronometre = new Chronometer();
            pushPause = false;

            Globals.Screen = new Vector2(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

            cam = new Camera2d(Globals.GDM, 0.42f);

            Globals.Camera = cam;

            Game game = Globals.Game;




            #region character1

            character1 = new Character(50, -1100)
            {
                Speedmax = new Vector2(540.0f, 4150.0f),
                Speedmin = new Vector2(100.0f, 50.0f),
                Inertia = 10,
                Gravity = 5.6f,
                Controller = new ControllerKeyboard()
            };
            AnimationDefinition imgBig = new AnimationDefinition("bigGhost2", new Microsoft.Xna.Framework.Point(2000, 2561), new Microsoft.Xna.Framework.Point(2, 1), 5, true);
            AnimationDefinition imgup = new AnimationDefinition("up", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgup2 = new AnimationDefinition("ghost/jump", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgdown = new AnimationDefinition("down", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgdown2 = new AnimationDefinition("ghost/jump", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgface = new AnimationDefinition("face", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgface2 = new AnimationDefinition("ghost/face", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgright = new AnimationDefinition("right", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(4, 1), 10, true);
            AnimationDefinition imgright2 = new AnimationDefinition("ghost/right", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(4, 1), 10, true);
            AnimationDefinition imgleft = new AnimationDefinition("left", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(4, 1), 10, true);
            AnimationDefinition imgleft2 = new AnimationDefinition("ghost/left2", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(4, 1), 10, true);
            AnimationDefinition imgfly = new AnimationDefinition("fly", new Microsoft.Xna.Framework.Point(120, 200), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imgattack = new AnimationDefinition("attack", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(2, 1), 20, true);
            AnimationDefinition imgdead = new AnimationDefinition("dead", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgfacecowboy = new AnimationDefinition("Cowboy/face", new Microsoft.Xna.Framework.Point(123, 150), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imgflycowboy = new AnimationDefinition("Cowboy/fly", new Microsoft.Xna.Framework.Point(156, 150), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imgrightcowboy = new AnimationDefinition("Cowboy/right", new Microsoft.Xna.Framework.Point(123, 150), new Microsoft.Xna.Framework.Point(14, 1), 60, true);
            AnimationDefinition imgleftcowboy = new AnimationDefinition("Cowboy/left", new Microsoft.Xna.Framework.Point(123, 150), new Microsoft.Xna.Framework.Point(14, 1), 60, true);


            character1.AddAnimation("default", new AnimationSprite(game, imgface));
            character1.AddAnimation("right", new AnimationSprite(game, imgright));
            character1.AddAnimation("left", new AnimationSprite(game, imgleft));
            character1.AddAnimation("up", new AnimationSprite(game, imgup));
            character1.AddAnimation("down", new AnimationSprite(game, imgdown));
            character1.AddAnimation("fly", new AnimationSprite(game, imgfly));
            character1.AddAnimation("dead", new AnimationSprite(game, imgdead));
            character1.AddAnimation("attack", new AnimationSprite(game, imgattack));

            character1.InitializeAnimations();

            ghost = new Ghost(-3500, -900);
            ghost.AddAnimation("default", new AnimationSprite(game, imgBig));
            ghost.AddAnimation("right", new AnimationSprite(game, imgBig));
            ghost.InitializeAnimations();
            ghost.Width = 2000;
            ghost.Height = 2561;

            character2 = new Character(250, -900)
            {
                Speedmax = new Vector2(500.0f, 5000.0f),
                Speedmin = new Vector2(1000.0f, 90.0f),
                Controller = new ControllerKeyboard2()
            };

            character2.AddAnimation("default", new AnimationSprite(game, imgface2));
            character2.AddAnimation("right", new AnimationSprite(game, imgright2));
            character2.AddAnimation("left", new AnimationSprite(game, imgleft2));
            character2.AddAnimation("up", new AnimationSprite(game, imgup2));
            character2.AddAnimation("down", new AnimationSprite(game, imgdown2));
            character2.AddAnimation("fly", new AnimationSprite(game, imgfly));
            character2.AddAnimation("dead", new AnimationSprite(game, imgdead));
            character2.AddAnimation("attack", new AnimationSprite(game, imgattack));


            character2.InitializeAnimations();


            #endregion

            // Pièces
            AnimationDefinition imgPieceOr = new AnimationDefinition("pieces/face", new Microsoft.Xna.Framework.Point(37, 72), new Microsoft.Xna.Framework.Point(2, 1), 2, true);
            AnimationDefinition imgPieceArgent = new AnimationDefinition("pieces/face_argent", new Microsoft.Xna.Framework.Point(37, 72), new Microsoft.Xna.Framework.Point(2, 1), 2, true);

            AnimationDefinition imgPieceBronze = new AnimationDefinition("pieces/face_bronze", new Microsoft.Xna.Framework.Point(37, 72), new Microsoft.Xna.Framework.Point(2, 1), 2, true);

            FactoryPiece factoryPiece = new FactoryPiece(imgPieceOr, imgPieceArgent, imgPieceBronze);

            #region Level

            leveltest = new Level();


            AnimationDefinition imglevel = new AnimationDefinition("grotte-background", new Microsoft.Xna.Framework.Point(3000, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            // Images du niveau 1
            AnimationDefinition imggrotte1 = new AnimationDefinition("Grotte 2/grotte.1-1-b", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte2 = new AnimationDefinition("Grotte 2/grotte.1-1-m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte3 = new AnimationDefinition("Grotte 2/grotte.1-1-f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imggrotte4 = new AnimationDefinition("Grotte 2/grotte.2-1-b", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte5 = new AnimationDefinition("Grotte 2/grotte.2-1-m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte6 = new AnimationDefinition("Grotte 2/grotte.2-1-f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imggrotte512 = new AnimationDefinition("Grotte 2/grotte.5_1-2-m", new Microsoft.Xna.Framework.Point(3501, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);


            AnimationDefinition imglevel0 = new AnimationDefinition("Grotte 2/grotte_lvl0", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte7 = new AnimationDefinition("Grotte 2/grotte.3-1-m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte8 = new AnimationDefinition("Grotte 2/grotte.3-1-f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imggrotte36_1 = new AnimationDefinition("Grotte 2/grotte.4_1-3-m", new Microsoft.Xna.Framework.Point(3001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte36_2 = new AnimationDefinition("Grotte 2/grotte.4_1-3-f", new Microsoft.Xna.Framework.Point(3001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);


            AnimationDefinition imggrotte9 = new AnimationDefinition("Grotte 2/grotte.4-1-m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte10 = new AnimationDefinition("Grotte 2/grotte.4-1-f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            // Images du niveau 2
            AnimationDefinition imggrotte21 = new AnimationDefinition("Grotte 2/grotte.1-2-b", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte22 = new AnimationDefinition("Grotte 2/grotte.1-2-m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte23 = new AnimationDefinition("Grotte 2/grotte.1-2-f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imggrotte24 = new AnimationDefinition("Grotte 2/grotte.2-2-b", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte25 = new AnimationDefinition("Grotte 2/grotte.2-2-m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte26 = new AnimationDefinition("Grotte 2/grotte.2-2-f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imggrotte27 = new AnimationDefinition("Grotte 2/grotte.3-2-m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte28 = new AnimationDefinition("Grotte 2/grotte.3-2-f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imggrotte29 = new AnimationDefinition("Grotte 2/grotte.4-2-b", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte292 = new AnimationDefinition("Grotte 2/grotte.4-2-m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte30 = new AnimationDefinition("Grotte 2/grotte.4-2-f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            // Images du niveau 3
            AnimationDefinition imggrotte31 = new AnimationDefinition("Grotte 2/grotte.1.3.m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte32 = new AnimationDefinition("Grotte 2/grotte.1.3.f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imggrotte33 = new AnimationDefinition("Grotte 2/grotte.2.3.b", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte34 = new AnimationDefinition("Grotte 2/grotte.2.3.m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte35 = new AnimationDefinition("Grotte 2/grotte.2.3.f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imggrotte532m = new AnimationDefinition("Grotte 2/grotte.5_3-2-m", new Microsoft.Xna.Framework.Point(3001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotte532f = new AnimationDefinition("Grotte 2/grotte.5_3-2-f", new Microsoft.Xna.Framework.Point(3001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            // Niveau de fin
            AnimationDefinition imggrotteend1 = new AnimationDefinition("grotte.end.m", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imggrotteend2 = new AnimationDefinition("grotte.end.f", new Microsoft.Xna.Framework.Point(2001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imgleveltest = new AnimationDefinition("Level tutorial/level1-1", new Microsoft.Xna.Framework.Point(3001, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);



            // J'ajoute une portion de niveau (Base, Background, Foreground)
            // niveau 1

            //leveltest.addFrame(imggrotte36_1, imglevel0, imggrotte36_2, game, 1, 3);

            Lava lava = new Lava(430, -620, 1800, 100);
            leveltest.addFrame(imggrotte512, imglevel0, imglevel0, game, 1, 2);//5.1

            Piece pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 2029, -900);
            leveltest.Lvl1[0].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceOr, 2389, -1312);
            leveltest.Lvl1[0].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceArgent, 2785, -1216);
            leveltest.Lvl1[0].listElement.Add((Piece)pieceOAB.Clone());
            
            leveltest.Lvl1[0].listElement.Add((Lava)lava.Clone());
            //leveltest.addFrame(imggrotte2, imggrotte1, imggrotte3, game, 1, 1);
            
            //lava = new Lava(1040, -710, 440, 100);
            //leveltest.Lvl1[1].listElement.Add((Lava)lava.Clone());

            leveltest.addFrame(imggrotte5, imggrotte4, imggrotte6, game, 1, 1); //2.1
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1197, -780);
            leveltest.Lvl1[1].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 109, -961);
            leveltest.Lvl1[1].listElement.Add((Piece)pieceOAB.Clone());
            lava = new Lava(730, -720, 750, 100);
            leveltest.Lvl1[1].listElement.Add((Lava)lava.Clone());

            leveltest.addFrame(imggrotte7, imglevel0, imggrotte8, game, 1, 2); //3.1

            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceOr, 1301, -1277);
            leveltest.Lvl1[2].listElement.Add((Piece)pieceOAB.Clone());

            Trampolline tramp = new Trampolline(1700, -870, 154, 117);
            AnimationDefinition imgtramp = new AnimationDefinition("trampolline", new Microsoft.Xna.Framework.Point(154, 117), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            tramp.AddAnimation("default", new AnimationSprite(Globals.Game, imgtramp));

            leveltest.addFrame(imggrotte9, imglevel0, imggrotte10, game, 1, 3);//4.1
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1481, -992);
            leveltest.Lvl1[3].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1821, -1250);
            leveltest.Lvl1[3].listElement.Add((Piece)pieceOAB.Clone());
            leveltest.Lvl1[3].listElement.Add((Trampolline)tramp.Clone());
            


            // niveau 2
            leveltest.addFrame(imggrotte22, imggrotte21, imggrotte23, game, 2, 2); //1.2

             pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 400, -1128);

             leveltest.Lvl2[0].listElement.Add((Piece)pieceOAB.Clone());
              pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceArgent, 400, -1400);
              leveltest.Lvl2[0].listElement.Add((Piece)pieceOAB.Clone());
              pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1745, -1350);
              leveltest.Lvl2[0].listElement.Add((Piece)pieceOAB.Clone());

            lava = new Lava(1040, -720, 440, 100);
            leveltest.Lvl2[0].listElement.Add((Lava)lava.Clone());

            leveltest.addFrame(imggrotte25, imggrotte24, imggrotte26, game, 2, 2); //2.2
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 913, -1285);
            leveltest.Lvl2[1].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceArgent, 1569, -1276);
            leveltest.Lvl2[1].listElement.Add((Piece)pieceOAB.Clone());
            lava = new Lava(730, -720, 740, 100);
            leveltest.Lvl2[1].listElement.Add((Lava)lava.Clone());

            leveltest.addFrame(imggrotte27, imglevel0, imggrotte28, game, 2, 3);//3.2
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1441, -1288);
            leveltest.Lvl2[2].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1861, -1450);
            leveltest.Lvl2[2].listElement.Add((Piece)pieceOAB.Clone());
            leveltest.addFrame(imggrotte292, imggrotte29, imggrotte30, game, 2, 2);//4.2
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1117, -1050);
            leveltest.Lvl2[3].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 433, -1350);
            leveltest.Lvl2[3].listElement.Add((Piece)pieceOAB.Clone());

            lava = new Lava(450, -700, 1500, 100);
            leveltest.Lvl2[3].listElement.Add((Lava)lava.Clone());

            // niveau 3
            leveltest.addFrame(imggrotte31, imglevel0, imggrotte32, game, 3, 3);//1.3
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 450, -1700);
            leveltest.Lvl3[0].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1017, -1476);
            leveltest.Lvl3[0].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceOr, 1900, -1700);
            leveltest.Lvl3[0].listElement.Add((Piece)pieceOAB.Clone());
            lava = new Lava(880, -340, 405, 100);
            leveltest.Lvl3[0].listElement.Add((Lava)lava.Clone());

            leveltest.addFrame(imggrotte34, imggrotte33, imggrotte35, game, 3, 1); //2.3
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1900, -850);
            leveltest.Lvl3[1].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1900, -950);
            leveltest.Lvl3[1].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceBronze, 1900, -1050);
            leveltest.Lvl3[1].listElement.Add((Piece)pieceOAB.Clone());
            lava = new Lava(1270, -690, 570, 100);
            leveltest.Lvl3[1].listElement.Add((Lava)lava.Clone());

            leveltest.addFrame(imggrotte532m, imglevel0, imggrotte532f, game, 3, 2); //5.3
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceOr, 961, -1555);
            leveltest.Lvl3[2].listElement.Add((Piece)pieceOAB.Clone());
            pieceOAB = factoryPiece.Create(PROJECT_BOMBER.BLL.FactoryPiece.TypePiece.PieceArgent, 1709, -1530);
            leveltest.Lvl3[2].listElement.Add((Piece)pieceOAB.Clone());
            lava = new Lava(440, -870, 1760, 100);
            leveltest.Lvl3[2].listElement.Add((Lava)lava.Clone());

            leveltest.GenerateLevel(5);


            LevelBloc anim = new LevelBloc(0, Globals.Game);

            anim.Background(imglevel0, game);
            anim.Base(imggrotteend1, game);
            anim.Foreground(imggrotteend2, game);

            winner = new Win(1806, -1170, 200, 200);

            AnimationDefinition winn = new AnimationDefinition("win", new Microsoft.Xna.Framework.Point(200, 147), new Microsoft.Xna.Framework.Point(1, 1), 2, true);

            winner.AddAnimation("default", new AnimationSprite(Globals.Game, winn));

            anim.listElement.Add((Win)winner.Clone());

            leveltest.Listframe.Add(anim);
            leveltest.Width += anim.Base().Definition.FrameSize.X;


            leveltest.InitializeAnimation();

            Globals.Level = leveltest;


            #endregion

            #region win


            winner = new Win(200, 1600, 200, 200);


            AnimationDefinition winny = new AnimationDefinition("win", new Microsoft.Xna.Framework.Point(200, 147), new Microsoft.Xna.Framework.Point(1, 1), 2, true);

            winner.AddAnimation("default", new AnimationSprite(Globals.Game, winny));

            winner.InitializeAnimations();


                /*pieces[i].Animations.Base(imgpiece, this);
                pieces[i].Animations.Base().Initialize();*/


            #endregion
        }

        public override void Update(float deltatime)
        {
            if (!Globals.Chronometre.Start)
            {
                Globals.Chronometre.Reset();
                Globals.Chronometre.Start = true;
            }
            Globals.Chronometre.Update();
            screenRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, Globals.GDM.PreferredBackBufferWidth, Globals.GDM.PreferredBackBufferHeight);

            #region listes d'element pour les collision element se deplaçant

            List<ElementPhysique> elements = new List<ElementPhysique>();
            //elements.Add(ghost);
            elements.Add(leveltest);


            foreach (var frame in leveltest.Listframe)
            {
                foreach (var item in frame.listElement)
                {
                    elements.Add(item);
                }
            }

            #endregion

            character1.Update(leveltest, elements);
            ((Ghost)ghost).Update(character1);


            //character2.Update(this, Globals.GameTime, Globals.GDM, leveltest, cam, elements, screen);

            cam.Update(Globals.GDM, character1);

            //piece.AnimationSprite.Base().Framerate = 1;

            leveltest.Update();
            //piece.AnimationSprite.Base().Framerate = 1;



            //ghost.CollisionStrategy.Collision(character1, ghost);




            text = string.Format("Vitesse : X = {0}   Y = {1}", character1.Speed.X.ToString(), character1.Speed.Y.ToString());
            text2 = string.Format("Temps : {0}", Globals.Chronometre.Seconde);
            text3 = string.Format("Score : {0} ", character1.Score);

            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.L))
            {
                Globals.GameStateStack.Set(new LoadState(new PlayState2()));
            }

            PauseState _pause = new PauseState();

            KeyboardState ks2 = Keyboard.GetState();

            if (character1.Dead == true)
            {
                Globals.GameStateStack.Set(new GameOverState());
            }

            if (ks2.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.P) && !pushPause)
            {
                pushPause = true;

                Globals.GameStateStack.Pause(_pause, this);
            }
            else if (ks2.IsKeyUp(Microsoft.Xna.Framework.Input.Keys.P))
            {
                pushPause = false;
            }

            if (ks2.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.M))
            {
                MediaPlayer.Pause();

            }

            if (ks2.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.U))
            {
                MediaPlayer.Resume();

            }
        }

        public override void Draw()
        {
            Globals.GDM.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Gray);

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            Globals.SpriteBatch.Draw(background, screenRectangle, Microsoft.Xna.Framework.Color.White);
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            leveltest.DrawAnimation(Globals.GameTime, 0);
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            leveltest.DrawAnimation(Globals.GameTime, 1);
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            character1.DrawAnimation();
            Globals.SpriteBatch.End();



            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            character2.DrawAnimation();
            Globals.SpriteBatch.End();




            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            leveltest.DrawAnimation(Globals.GameTime, 2);
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            ghost.DrawAnimation();
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            Globals.SpriteBatch.DrawString(spritetext, text, Vector2.Zero, Microsoft.Xna.Framework.Color.Red);
            Globals.SpriteBatch.DrawString(spritetext, text2, new Vector2(400, 0), Microsoft.Xna.Framework.Color.Red);
            Globals.SpriteBatch.DrawString(spritetext, text3, new Vector2(800, 0), Microsoft.Xna.Framework.Color.Red);



            Globals.SpriteBatch.End();
        }

        private Vector2 PlayTextPosition = Vector2.Zero;


        public override void Load()
        {

            musique = Globals.Content.Load<Song>("Music");

            soundPiece = Globals.Content.Load<SoundEffect>("Coin");

            MediaPlayer.Play(musique);
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Volume = 0.8f;


            AspectRatio = Globals.GDM.GraphicsDevice.Viewport.AspectRatio;
            OldWindowSize = new Microsoft.Xna.Framework.Point(Globals.Window.ClientBounds.Width, Globals.Window.ClientBounds.Height);

            // Create a spritetext, which can be used to write.
            spritetext = Globals.Content.Load<SpriteFont>("Arial");

            background = Globals.Content.Load<Texture2D>("grotte-background");
            test = Globals.Content.Load<Texture2D>("test");

            character1.LoadAnimations(Globals.SpriteBatch);
            ghost.LoadAnimations(Globals.SpriteBatch);
            character2.LoadAnimations(Globals.SpriteBatch);


            leveltest.LoadAnimations(Globals.SpriteBatch);

        }

        public override void Unload()
        {

            Globals.Content.Unload();
        }
    }
}
